//
//  QLForgetPasswordVC.m
//  神约
//
//  Created by qlwl on 16/10/27.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "QLForgetPasswordVC.h"
#import "NSString+Judge.h"
#import "NSString+MD5.h"

@interface QLForgetPasswordVC ()
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *passwordAgainField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation QLForgetPasswordVC
{
    NSTimer *_timer;
    int _timing;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavTitle:@"找回密码"];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(registerVCTimoGo) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];
}

- (IBAction)codeBtnClick:(id)sender {
    [self getCodeNet];
}
- (IBAction)retrievePasswordBtnClick:(id)sender {
    [self retrievePasswordNet];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark timer
//开始计时
- (void)timingBtnState
{
    [_timer setFireDate:[NSDate date]];
    self.codeBtn.userInteractionEnabled=NO;
    self.codeBtn.selected=YES;
}
- (void)registerVCTimoGo
{
    if (_timing>=59) {
        _timing=0;
        self.codeBtn.userInteractionEnabled=YES;
        self.codeBtn.selected=NO;
        [self.codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];
    }else{
        _timing++;
        [self.codeBtn setTitle:[NSString stringWithFormat:@"等待(%ds)",60-_timing] forState:UIControlStateNormal];
    }
}


- (void) getCodeNet
{
    if (![NSString validateMobile:self.phoneField.text]) {
        return;
    }
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"mobilephone"] = self.phoneField.text;
    postDict[@"type"] = [NSNumber numberWithInteger:2];
    [QLNetWorking postJSONWithUrl:QLSendVerificationCodeURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            [self timingBtnState];
        }else{
            [LPPopup showInCenterOfWindowWithText:[[result objectForKey:@"mobileHead"] objectForKey:@"message"] completion:nil];
        }
    } fail:^(NSError *error) {
        
    }];
}

- (void)retrievePasswordNet
{
    if (![NSString validateMobile:self.phoneField.text]) {
        return;
    }
    if (!self.codeField.text.length) {
        return;
    }
    if (!self.passwordField.text.length) {
        return;
    }
    if (![self.passwordField.text isEqualToString:self.passwordAgainField.text]) {
        return;
    }
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"mobilephone"] = self.phoneField.text;
    postDict[@"code"] = self.codeField.text;
    postDict[@"password"] = [NSString MD5WithString:self.passwordField.text];
    [QLNetWorking postJSONWithUrl:QLRetrievePasswordURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        [LPPopup showInCenterOfWindowWithText:[[result objectForKey:@"mobileHead"] objectForKey:@"message"] completion:nil];
    } fail:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
